package com.action.login;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.entity.User;
import com.enums.Day;
import com.enums.Month;
import com.enums.Year;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.service.login.LoginService;
import com.util.email.DesUtils;
import com.util.email.Emailsender;
import com.util.email.ErrorMailInterface;
import com.util.email.MailConfirmUtils;
import com.util.pagination.Pagination;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 7697721996820608961L;
	
	/*************************************************attributes*******************************************************/
	
	@Autowired         
	private LoginService loginService;
	
	private User user;
	
	private List<User> userList;
	
	private String email="";
	
	private String password="";
	
	private String realName;
	
	private List<Year> yearList;
	
	private List<Month> monthList;
	
	private List<Day> dayList;
	
	private int year;
	
	private int month;
	
	private int day;
	
	private String data;
	
	private String reDate;
	
	private Pagination page;

    private int pageSize =5;

    private int pageNo = 1;

	/************************************************* methods*******************************************************/
	
	//跳转到登入页面
	public String toLogin(){
		return "toLogin";
	}
	
	
	//跳转到确认页面
	public String toConfirm(){
		return "toConfirm";
	}
	
	//跳转到分页
	public String toPageTest(){
		 page = new Pagination(201, pageSize, pageNo);
		return "toPageTest";
	}
	
	//登入
	public String login(){
		try{
				userList=loginService.getUserByUsername(email);
				if(userList.size()==0)
					return "loginError";
				else{
					ActionContext.getContext().getSession().put("user", userList.get(0));
				}
		}catch (Exception e) {
			e.printStackTrace();
			return "loginError";
		}
		return "loginSuccess";
	}
	
	//跳转到注册页面
	public String toRegister(){
		yearList=Year.getYearList();
		monthList=Month.getMonthList();
		dayList=Day.getdayList();
		return "toRegister";
	}
	
	//注册方法
	public String register(){
		try{
			 ErrorMailInterface errormail = new MailConfirmUtils(user);
			    Emailsender es = new Emailsender();
			    if (es.sendMail(errormail)) {
			           System.out.println("发送成功");
			           try{
				        	Date date=new Date();
				   			date.setYear(year-1);
				   			date.setMonth(month-1);
				   			date.setDate(day);
				   			user.setBirth(date);
				   			user.setMailOrNot(0);
				   			loginService.saveUser(user);
			           }catch (Exception e) {
			        	   e.printStackTrace();
				        }
			       } else {
			           System.out.println("发送失败");
			           return "error";
			       }
			
		}catch (Exception e) {
			e.printStackTrace();
			return "registerError";
		}
		return "registerSuccess";
	}
	
	//重新发送确认
	public String reSendEmail(){
		   user=new User();
		   user.setEmail(email);
		   user.setPassword(password);
	       ErrorMailInterface errormail = new MailConfirmUtils(user);
	       Emailsender es = new Emailsender();
	       if (es.sendMail(errormail)) {
	           System.out.println("重新发送成功");
	       } else {
	           System.out.println("重新发送失败");
	       }
	       return "registerSuccess";
	}
	
	//激活后保存用户
	public String saveUser(){
		System.out.println(data);
		DesUtils des;
		String beforeDate=new String();
		DateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		java.util.Date date1;
		java.util.Date date2;
		user=new User();
		try {
			    date2=dFormat.parse(dFormat.format(new java.util.Date()).toString());
	            des = new DesUtils();
	            reDate=data;
	            data = des.decrypt(data);
	            String[] mes = new String[20];
	            mes = data.split("&");
	            email=mes[0];
	            password=mes[1];
	            beforeDate=mes[2];
	            date1=dFormat.parse(beforeDate);
	            user=new User();
	            userList=loginService.getUserByUsername(mes[0].trim());
	            user=userList.get(0);
	            long l=date2.getTime()-date1.getTime();
	            long day=l/(24*60*60*1000);
	            long hour=(l/(60*60*1000)-day*24);
	            long min=((l/(60*1000))-day*24*60-hour*60);
	            if(day>1)
	            	return "reCofirm";
	            else if(hour>1)
	            	return "reCofirm";
	            else if(min>30)
	            	return "reCofirm";
	           /* else if(s>10)
	            	return "reCofirm";*/
	          //  System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");
	            user.setMailOrNot(1);
	            loginService.updateUser(user);
	        } catch (Exception e) {
	            System.out.println("Mail加密错误");
	            e.printStackTrace();
	        }
	       ActionContext.getContext().getSession().put("user", user);
		return "save";
	}
	
	
	/*************************************************getter and setter methods*******************************************************/


	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public List<Year> getYearList() {
		return yearList;
	}

	public void setYearList(List<Year> yearList) {
		this.yearList = yearList;
	}

	public List<Month> getMonthList() {
		return monthList;
	}

	public void setMonthList(List<Month> monthList) {
		this.monthList = monthList;
	}

	public List<Day> getDayList() {
		return dayList;
	}

	public void setDayList(List<Day> dayList) {
		this.dayList = dayList;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String getReDate() {
		return reDate;
	}

	public void setReDate(String reDate) {
		this.reDate = reDate;
	}


	public Pagination getPage() {
		return page;
	}


	public void setPage(Pagination page) {
		this.page = page;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public int getPageNo() {
		return pageNo;
	}


	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	
	
}
